$(document).ready(function() {
	chart_js.init();
	$("#page_title").html("分析数据曲线");
});
var chart_js = {};
chart_js.bind = function(){
	//滚动加载数据
	$(window).scroll(function() {
		var scrollTop = $(this).scrollTop();
		if(scrollTop+$(window).height()>=$(document).height()){
			vm.cur_page++;
			vm.load_mores();
		}
	});			
}
chart_js.init = function(){
	chart_js.bind();
	vm.load();
	vm.load_mores();
//	chart_js.load();
}
/**
 * 收缩压<舒张压
 * @param {Object} flag true代表是血压
 * @param {Object} show_text 显示的名称，如果是血压的话就直接传入null就行了
 * @param {Object} dates 时间数据的集合
 * @param {Object} first_data 如果是血压的话就是收缩压的数据，普通的就是全部数据
 * @param {Object} second_data 这里只能是血压的舒张压的数据
 * @param {Object} start_scale X轴的开始点
 * @param {Object} end_scale Y轴的结束点
 * @param {Object} scale_space 每个值的间距
 */
chart_js.load = function(flag,show_text,dates,first_data,second_data,start_scale,end_scale,scale_space) {
	console.log(arguments);
	//数据对象
	var data = [];
	if(flag){
		data = [{
			name: '收缩压',
			value: first_data,
			color: '#aad0db',
			line_width: 2
		}, {
			name: '舒张压',
			value: second_data,
			color: '#f68f70',
			line_width: 2
		}];
	}else{
		data = [{
			name: show_text,
			value: first_data,
			color: '#aad0db',
			line_width: 2			
		}];
	}
	var bodyWidth = $(window).width();
	var chart = new iChart.Area2D({
		render: 'canvas',
		data: data,
		align: 'center',
		width: bodyWidth-32,
		padding: 10,
		height: 340,
		background_color: '#FEFEFE',
		border: {
			enable: false,
		},
		tip: {
			enable: true,
			shadow: false,
			border: {
				enable: true,
				radius: 6,
				width: 0,
				color: '#3f8695'
			},
			listeners: {
				//tip:提示框对象、name:数据名称、value:数据值、text:当前文本、i:数据点的索引
				parseText: function(tip, name, value, text, i) {
					return name + ":" + value;
				}
			},
			style:'background: rgba(0,0,0,0.7);color:white;padding:4px;',
		},
		tipMocker: function(tips, i) {
			return "<div>时间：" +
				dates[i] + //时间
				"</div>" + tips.join("<br/>");
		},
		legend: {
			enable: true,
			row: 1, //设置在一行上显示，与column配合使用
			column: 'max',
			valign: 'top',
			align: 'center',
			sign: 'bar',
			background_color: null, //设置透明背景
			border: true,
			offsety: 0
		},
		crosshair: {
			enable: true,
			line_color: '#62bce9', //十字线的颜色
		},
		sub_option: {
			label: false,
			point_size: 10
		},
		coordinate: {
			padding: 0,
			width: '80%',
			height: 240,
			axis: {
				color: '#dcdcdc',
				width: 1
			},
			scale: [{
				position: 'left',
				//X轴的开始点 0
				start_scale: start_scale,
				//Y轴的结束点 100
				end_scale: end_scale,
				//每个值的间距 10
				scale_space: scale_space,
				//刻度线的粗细
				scale_size: 2,
				scale_color: '#9f9f9f'
			}]
		}
	});
	console.log(111);
	//开始画图
	chart.draw();
}
var vm = avalon.define({
	//标记是否显示遮罩
	is_mask_show:false,	
	$id:"main_controller",
	//----------------
	//toast的信息
	toast_msg:'',
	//是否要展示toast
	is_toast_show:false,
	//定时器
	$timeout:0,
	//消息隐藏
	clear_toast:function(){
		//设置定时器
		if(vm.$timeout){
			clearTimeout(vm.$timeout);
		}
		vm.$timeout = setTimeout(function(){
			vm.is_toast_show = false;
			vm.toast_msg = '';
		},3000);		
	},	
	//-----------------
	active_index:0,
	//这里是假定只有三个数据的
	date_values:[
		{
			text:"周",
			datestatus:1,
		},
		{
			text:"月",
			datestatus:2,
		},
		{
			text:"季",
			datestatus:3,
		},		
	],
	/**
	 * 点击加载数据 
	 * @param {Object} index
	 */
	do_load_data:function(index){
		vm.active_index = index;
		vm.datestatus = vm.date_values[index].datestatus;
		vm.load();
		vm.cur_page = 1;
		vm.list_mores = [];
		vm.load_mores();
	},
	//标记当前是否是血压
	flag:common.getUrlParam('flag'),
	//传过来的单位
	unit:decodeURI(common.getUrlParam('unit')),
	//名称
	testname:decodeURI(common.getUrlParam('testname')),
	//最大值
	limithigh:new Number(common.getUrlParam('limithigh')),
	//最小值
	limitlow:new Number(common.getUrlParam('limitlow')),
	//chart数据
	list:[],
	//单项的数据
	list_mores:[],
	//表示是否加载单项的数据
	is_loading:false,
	//当前页
	cur_page:1,
	//时间状态 1.周 2.月 3.季
	datestatus:1,
	//最大值
	max:'0',
	//最小值
	min:'0',
	//平均值
	avg:'0',
	//标记是否要进行处理表格
	is_do:common.getUrlParam('is_do'),
	//加载所有的chart数据
	load:function(){
		console.log(vm.is_do);
		if(vm.is_do == 'false' || vm.is_do == false){
			return ;
		}
		vm.is_mask_show = true;
		$.ajax({
			type:"get",
			url:get_health_info_url,
			dataType:"json",
			data:"dicttestitemid="+common.getUrlParam('dicttestitemid')+"&datestatus="+vm.datestatus,
			success:function(data){
				vm.is_mask_show = false;
				console.log(data);
				var dates = [],
					first_data = [],
					second_data = [],
					start_scale = 0,
					end_scale = 0,
					scale_space = 0,
					count_1 = 0,
					count_2 = 0;
				if(data.length === 0){
					chart_js.load(vm.flag,vm.testname,[''],[0],[0],0,100,10);
					return ;
				}
				$.each(data, function(index) {    
					dates.push(data[index].enterdate);
					//testresult
					if(new Boolean(vm.flag)==true){
						var datas = data[index].testresult.split('/');
						first_data.push(datas[0]);
						second_data.push(datas[1]);
						count_1 += new Number(datas[0]);
						count_2 += new Number(datas[1]);
					}else{
						count_1 += new Number(data[index].testresult);
						first_data.push(data[index].testresult);
					}
				});
				if(new Boolean(vm.flag)==true){
					var max_value_1 = Math.max.apply(Math,first_data);
					var max_value_2 = Math.max.apply(Math,second_data);
					var min_value_1 = Math.min.apply(Math,first_data);
					var min_value_2 = Math.min.apply(Math,second_data);
					vm.max = max_value_1+"/"+max_value_2;
					vm.min = min_value_1+"/"+min_value_2;
					vm.avg = (count_1/data.length).toFixed(2)+"/"+(count_2/data.length).toFixed(2);
				}else{
					vm.avg = (count_1/data.length).toFixed(2);
					vm.max = Math.max.apply(Math,first_data);
					vm.min = Math.min.apply(Math,first_data);
				}
				end_scale = vm.limithigh;
				start_scale = vm.limitlow;		
				scale_space = (end_scale-start_scale)/10;
				console.log('dates',dates);
				console.log('first_data',first_data);
				console.log('first_data',second_data);
				console.log('first_data',second_data);
				console.log('end_scale',new Number(end_scale));
				console.log('start_scale', new Number(start_scale));
				console.log('scale_space', new Number(scale_space));
				chart_js.load(vm.flag,vm.testname,dates,first_data,second_data,(start_scale+0),(end_scale+0),(scale_space+0));
			},
			error:function(){
				vm.is_mask_show = false;
				vm.toast_msg = "网络错误";
				vm.is_toast_show = true;
				vm.clear_toast();		
			}
		});			
	},
	//加载单项的数据
	load_mores:function(){
		if(vm.is_loading==true){
			return ;
		}
		vm.is_loading = true;
		$.ajax({
			type:"get",
			url:get_health_info_page_url,
			dataType:"json",
			data:"cur_page="+vm.cur_page+"&dicttestitemid="+common.getUrlParam('dicttestitemid')+"&datestatus="+vm.datestatus,
			success:function(data){
				console.log(data);
				vm.is_loading = false;
				vm.list_mores.pushArray(data);
				if(!data || data.length<=0){
					//这里重新处理页数
					if(vm.cur_page!=1){
						vm.cur_page--;
					}
				vm.toast_msg = "没有更多数据了";
				vm.is_toast_show = true;
				vm.clear_toast();						
				}
			},
			error:function(){
				vm.is_loading = false;
				vm.toast_msg = "网络错误";
				vm.is_toast_show = true;
				vm.clear_toast();		
			}
		});		
	},
});
avalon.scan();
avalon.ready(function(){
	$("body").css('opacity',1);
});